import { CallHandler, ExecutionContext, Injectable, Logger, NestInterceptor } from '@nestjs/common'
import { Observable } from 'rxjs'
import { map } from 'rxjs/operators'
import { Request } from 'express'
import config from 'src/config'
import AesUtils from 'src/utils/aes'
import { IRequest } from 'src/interface/common'
import { Response as Res } from 'express';
import * as moment from 'moment'
import { getCurrentDate } from 'src/app/utils'

interface Response<T> {
  data: T
}

@Injectable()
export class ResponseInterceptor<T> implements NestInterceptor<T, Response<T>> {
	intercept(context: ExecutionContext, next: CallHandler<T>): Observable<any> {
		const request = context.switchToHttp().getRequest<IRequest>()
		const response = context.switchToHttp().getResponse<Res>()

		return next.handle().pipe(
			map(data => {
				response.setHeader('Access-Control-Expose-Headers', 'X-Server-Time');
				response.setHeader('Access-Control-Expose-Headers', 'Accept-Language');
				response.setHeader("X-Server-Time",moment().valueOf())
				if(request.path.startsWith('/app')){
					return data
				}
				return {
					data: data,
					code: 0,
					message: '请求成功'
				}
			})
		)
	}
}
